/* PhotoImport
 * SettingsWindow class presents an UI for user to set settings
 * Copyright by faena.com
 * 2010-08-17 VM
 * 
*/
using System;
using Gtk;
using System.Xml;
using System.Text;

namespace PhotoImport
{
	public partial class SettingsWindow : Gtk.Dialog
	{


		/// <summary>
		/// Save settings when OK button is clicked 
		/// </summary>
		/// <param name="sender">
		/// A <see cref="System.Object"/>
		/// </param>
		/// <param name="e">
		/// A <see cref="System.EventArgs"/>
		/// </param>
		protected virtual void OnButtonOkClicked (object sender, System.EventArgs e)
		{
			SettingFile settingFile = new SettingFile ();
			
			settingFile.MediaTypes = uxMediaTypes.Text;
			
			settingFile.DeleteAfterDownload = uxDeleteAfterDownload.Active;
			
					
			
			settingFile.TargetDir = uxTargetDir.Filename;
			
			settingFile.CreateSubDir = uxCreateSubDir.Active;
			
			settingFile.SubfolderTemplate = (SettingFile.SubfolderTemplates)uxSubfolderTemplate.Active;
			
			settingFile.NewName = uxNewName.Text;
			
			
			if (uxUseShootTime.Active)
				settingFile.UseTime = SettingFile.UseTimes.UseShotTime;
			
			if (uxUseCurrentTime.Active)
				settingFile.UseTime = SettingFile.UseTimes.UseCurrentTime;
			
			settingFile.Save ();
			
			
			
		}

		/// <summary>
		/// Class constructor, called when open Settings window. 
		/// </summary>
		public SettingsWindow ()
		{
			this.Build ();
			
			SettingFile sFile = new SettingFile ();
			sFile.Reload ();
			
			uxMediaTypes.Text = sFile.MediaTypes;
			
			uxDeleteAfterDownload.Active = sFile.DeleteAfterDownload;
			
					
			
			uxTargetDir.SetFilename(sFile.TargetDir);
			
			uxCreateSubDir.Active = sFile.CreateSubDir;
			
			uxSubfolderTemplate.Active = (int)sFile.SubfolderTemplate;
			
			uxNewName.Text = sFile.NewName;
			
			
			switch (sFile.UseTime) {
			case SettingFile.UseTimes.UseShotTime:
				uxUseShootTime.Active = true;
				break;
			case SettingFile.UseTimes.UseCurrentTime:
				uxUseCurrentTime.Active = true;
				break;
			default:
				throw new Exception(string.Format("Program error. Unknown filetimestamp:{0}" , sFile.UseTime));
			}
			
		}

		/// <summary>
		/// When Subfolder tmeplate changed 
		/// </summary>
		/// <param name="sender">
		/// A <see cref="System.Object"/>
		/// </param>
		/// <param name="e">
		/// A <see cref="System.EventArgs"/>
		/// </param>
		protected virtual void OnUxSubfolderTemplateChanged (object sender, System.EventArgs e)
		{
			ShowExample ();
		}

		/// <summary>
		/// Show an example for varis subfolder templates
		/// </summary>
		private void ShowExample ()
		{
			string targetDir = uxTargetDir.Filename;
			
			switch ((SettingFile.SubfolderTemplates)uxSubfolderTemplate.Active) {
			case  SettingFile.SubfolderTemplates.YearMonthDate: //0:
				//YearMonthDate
				uxExample.Text = "Example:\n-" + targetDir + "\n--" + DateTime.Today.ToString ("yyyy_MM_dd");
				break;
			case SettingFile.SubfolderTemplates.NewName: //1:
				//New Name
				uxExample.Text = "Example:\n-" + targetDir + "\n--" + uxNewName.Text;
				break;
			case SettingFile.SubfolderTemplates.Year_YearMonthDate: //2:
				//Year\YearMonthDate
				uxExample.Text = "Example:\n-" + targetDir + "\n--" + DateTime.Today.ToString ("yyyy") + "\n---" + DateTime.Today.ToString ("yyyy_MM_dd");
				break;
			case SettingFile.SubfolderTemplates.Year_NewName: //3:
				//Year\New Name
				uxExample.Text = "Example:\n-" + targetDir + "\n--" + DateTime.Today.ToString ("yyyy") + "\n---" + uxNewName.Text;
				break;
			case SettingFile.SubfolderTemplates.NewName_YearMonthDate: // 4:
				//New Name\YearMonthDate
				uxExample.Text = "Example:\n-" + targetDir + "\n--" + uxNewName.Text + "\n--" + DateTime.Today.ToString ("yyyy_MM_dd");
				break;
			case SettingFile.SubfolderTemplates.Year_Month_YearMonthDate: // 5:
				//Year\Month\YearMonthDate
				uxExample.Text = "Example:\n-" + targetDir + "\n--" + DateTime.Today.ToString ("yyyy") + "\n---" + DateTime.Today.ToString ("MM") + "\n----" + DateTime.Today.ToString ("yyyy_MM_dd");
				break;
			case SettingFile.SubfolderTemplates.Year_Month: // 6:
				//Year\Month
				uxExample.Text = "Example:\n-" + targetDir + "\n--" + DateTime.Today.ToString ("yyyy") + "\n---" + DateTime.Today.ToString ("MM");
				break;
			case SettingFile.SubfolderTemplates.YearMonthDate_NewName: // 7:
				//YearMonthDate\New Name
				uxExample.Text = "Example:\n-" + targetDir + "\n--" + DateTime.Today.ToString ("yyyy_MM_dd") + "\n--" + uxNewName.Text;
				break;
			default:
				throw new Exception(string.Format("Program error. Unknown Example:{0}" , uxSubfolderTemplate.Active));
			}
		}

		/// <summary>
		/// When New name changed
		/// </summary>
		/// <param name="sender">
		/// A <see cref="System.Object"/>
		/// </param>
		/// <param name="e">
		/// A <see cref="System.EventArgs"/>
		/// </param>
		protected virtual void OnUxNewNameChanged (object sender, System.EventArgs e)
		{
			ShowExample ();
		}
		
		/// <summary>
		/// When target dir changed 
		/// </summary>
		/// <param name="sender">
		/// A <see cref="System.Object"/>
		/// </param>
		/// <param name="e">
		/// A <see cref="System.EventArgs"/>
		/// </param>
		protected virtual void OnUxTargetDirSelectionChanged (object sender, System.EventArgs e)
		{
			ShowExample ();
			
		}
		
		
		
		
		
	}
}

